package application.common.util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.jfinal.ext.kit.DateKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.sun.jna.NativeLong;
import application.common.render.NET_DVR_DEVICE;

public class CamreaUtil {
	protected final static Log log = Log.getLog(CamreaUtil.class);

	public static void startRecordCamrea() {
		if (ShiroUtil.getConfig() != null && StrKit.equals("1", ShiroUtil.getConfig().getIsVideotape())
				&& !StrKit.isBlank(ShiroUtil.getConfig().getVideotapePath())) {
			if (ShiroUtil.getCameras() != null && ShiroUtil.getCameras().size() > 0) {
				for (NET_DVR_DEVICE camera : ShiroUtil.getCameras()) {
					HCNetSDK.NET_DVR_CLIENTINFO lpPreviewInfo = new HCNetSDK.NET_DVR_CLIENTINFO();
					lpPreviewInfo.lChannel = new NativeLong(1);

					NativeLong m_lRealHandle;// 预览句柄
					m_lRealHandle = HCNetSDK.INSTANCE.NET_DVR_RealPlay_V30(camera.getlUserID(), lpPreviewInfo, null,
							null, true);
					camera.setM_lRealHandle(m_lRealHandle);
					if (camera.getM_lRealHandle().longValue() < 0) {
						log.info("【" + camera.getCamera().getName() + "】预览失败");
						camera.setM_bRecord(false);
						return;
					}
					boolean isOK = HCNetSDK.INSTANCE.NET_DVR_MakeKeyFrame(camera.getlUserID(), lpPreviewInfo.lChannel);
					if (!isOK) {
						log.info("【" + camera.getCamera().getName() + "】主码流失败！");
						camera.setM_bRecord(false);
						HCNetSDK.INSTANCE.NET_DVR_StopRealPlay(camera.getM_lRealHandle());
						return;
					}
					FiltUtil.mkdir(String.format("%s\\%s\\", ShiroUtil.getConfig().getVideotapePath(),
							DateKit.toStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd")));
					boolean isSaveRealData = HCNetSDK.INSTANCE.NET_DVR_SaveRealData(m_lRealHandle,
							String.format("%s\\%s\\%s-%s.mp4", ShiroUtil.getConfig().getVideotapePath(),
									DateKit.toStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd"),
									camera.getCamera().getIp(),
									DateKit.toStr(new Date(System.currentTimeMillis()), "HH-mm-ss")));
					if (!isSaveRealData) {
						log.info("【" + camera.getCamera().getName() + "】设置录像地址失败！");
						camera.setM_bRecord(false);
						HCNetSDK.INSTANCE.NET_DVR_StopRealPlay(camera.getM_lRealHandle());
						return;
					}
					camera.setM_bRecord(true);
				}
			}
		}
	}

	public static void stopRecordCamrea() {
		if (ShiroUtil.getConfig() != null && StrKit.equals("1", ShiroUtil.getConfig().getIsVideotape())
				&& !StrKit.isBlank(ShiroUtil.getConfig().getVideotapePath())) {
			if (ShiroUtil.getCameras() != null && ShiroUtil.getCameras().size() > 0) {
				for (NET_DVR_DEVICE camera : ShiroUtil.getCameras()) {
					if (camera.isM_bRecord()) {
						HCNetSDK.INSTANCE.NET_DVR_StopSaveRealData(camera.getM_lRealHandle());
						HCNetSDK.INSTANCE.NET_DVR_StopRealPlay(camera.getM_lRealHandle());
						camera.setM_bRecord(false);
						camera.setM_lRealHandle(new NativeLong(-1));
					}
				}
			}
		}
	}

//	public static void remove() throws ParseException {
//		if (ShiroUtil.getConfig() != null && StrKit.equals("1", ShiroUtil.getConfig().getIsVideotape())
//				&& !StrKit.isBlank(ShiroUtil.getConfig().getVideotapePath())) {
//			File videotapePath = new File(ShiroUtil.getConfig().getVideotapePath());
//			if (videotapePath.exists()) {
//				File[] files = videotapePath.listFiles();
//				if (files != null) {
//					for (File fileChildDir : files) {
//						if (fileChildDir.isDirectory()) {
//							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//							Date date1 = sdf.parse(subMonth(fileChildDir.getName()));
//							Date date2 = new Date(System.currentTimeMillis());
//							if (date1.before(date2)) {
//								FiltUtil.deleteDir(new File(ShiroUtil.getConfig().getVideotapePath()+"//"+fileChildDir.getName()));
//								FiltUtil.doDeleteEmptyDir(ShiroUtil.getConfig().getVideotapePath()+"//"+fileChildDir.getName());
//							}
//						}
//					}
//				}
//			}
//		}
//	}

	/****
	 * 传入具体日期 ，返回具体日期增加一个月。
	 * 
	 * @param date 日期(2017-04-13)
	 * @return 2017-05-13
	 * @throws ParseException
	 */
	public static String subMonth(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dt = sdf.parse(date);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.MONTH, 3);
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;
	}

	public static void main(String[] args) throws ParseException {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = sdf.parse(subMonth("2021-02-27"));
			Date date2 = new Date(System.currentTimeMillis());

			System.out.println("date1 : " + sdf.format(date1));
			System.out.println("date2 : " + sdf.format(date2));


			if (date1.before(date2)) {
				System.out.println("Date1 时间在 Date2 之前");
				FiltUtil.deleteDir(new File("d://2021-02-27"));
				FiltUtil.doDeleteEmptyDir("d://2021-02-27");
			}

			if (date1.equals(date2)) {
				System.out.println("Date1 时间与 Date2 相等");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
